﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ComponentGo.Web;
using WebSchedulerDemo.PlanDataSetTableAdapters;

namespace WebSchedulerDemo.db {
	public partial class PlanItem : System.Web.UI.Page {
		PlanDataSet.PlanItemDataTable table;
		PlanDataSet.PlanItemRow row;

		protected void Page_Load(object sender, EventArgs e) {
			Response.Cache.SetCacheability(HttpCacheability.NoCache);

			if (Request["id"]=="null")
				Server.Transfer("default.aspx");

			if (string.IsNullOrEmpty(Request["id"])) {
				table = new PlanDataSet.PlanItemDataTable();
				row = table.NewPlanItemRow();
				row.DateBegin = JScriptTools.GetDate(long.Parse(Request["dt"]));
				row.DateEnd = row.DateBegin.AddDays(1);
				row.Project_ID = int.Parse(Request["row"]);
				row.Name = "New item";
				row.Busy = false;
				row.Important = false;
				table.AddPlanItemRow(row);
				IDLabel.Text = "New item";
			}
			else {
				int id = int.Parse(Request["id"]);
				using (PlanItemTableAdapter adapter = new PlanItemTableAdapter()) {
					table = adapter.GetDataByID(id);
					row = table[0];
				}
				IDLabel.Text = id.ToString();
			}
			if (!IsPostBack) {
				NameText.Text = row.Name;
				DateBeginText.Text = row.DateBegin.ToString();
				DateEndText.Text = row.DateEnd.ToString();
				ImportantCheck.Checked = row.Important;
				BusyCheck.Checked = row.Busy;
				if (row.IsColorNull())
					ColorDropList.SelectedIndex = 0;
				else
					ColorDropList.SelectedValue = row.Color;
			}
		}

		protected void SaveButton_Click(object sender, EventArgs e) {
			row.Name = NameText.Text;
			row.DateBegin = DateTime.Parse(DateBeginText.Text);
			row.DateEnd = DateTime.Parse(DateEndText.Text);
			row.Important = ImportantCheck.Checked;
			row.Busy = BusyCheck.Checked;
			if (ColorDropList.SelectedIndex==0)
				row.SetColorNull();
			else
				row.Color = ColorDropList.SelectedValue;

			using (PlanItemTableAdapter adapter = new PlanItemTableAdapter())
				adapter.Update(table);

			Response.Redirect("default.aspx");
		}
	}
}
